var task_config = {

};

// 开始生成话术
function startGenScript(){
    if(get_current_task()['current_product'] == ''){
        return alert('请选择产品');
    }

    $.ajax({
        url: '/startGenScript',
        type: 'POST',
        data: {
            'scene_idx':get_current_task()['scene_name']
        },
        success: function(response) {
            if(response.success){
                $('#startGenScript').text('正在生成' + get_current_task()['current_product'] + '的话术...');

                get_current_task()['startGenScript'] = true;
            } else {
                alert(response.message)
            }
        }
    });
}

// 暂停生成话术
function stopGenScript(){
    $.ajax({
        url: '/stopGenScript',
        type: 'POST',
        data: {
            'scene_idx':get_current_task()['scene_name']
        },
        success: function(response) {
            $('#startGenScript').text('开始生成话术');

            get_current_task()['startGenScript'] = false;
        }
    });
}

// 清空话术
function clearScript(){
    $.ajax({
        url: '/clearScript',
        type: 'POST',
        data: {
            'scene_idx':get_current_task()['scene_name']
        },
        success: function(response) {
            if(response.success){
                $('#clearScript').text('已清空' + get_current_task()['current_product'] + '的话术');
            }
        }
    });
}

// 切换产品
function selectProduct(){
    if(!$('#productSelect').val()){
        return;
    }

    $.ajax({
        url: '/selectProduct',
        type: 'POST',
        data: {
            scene_idx : get_current_task().scene_name,
            product: $('#productSelect').val()
        },
        success: function(response) {
            if(response.success){
                get_current_task().current_product = $('#productSelect').val();
            }
        }
    });
}

// 切换音色
function selectStreamer(){
    if(!$('#streamerSelect').val()){
        return;
    }

    $.ajax({
        url: '/selectStreamer',
        type: 'POST',
        data: {
            scene_idx : get_current_task().scene_name,
            streamer: $('#streamerSelect').val()
        },
        success: function(response) {
            if(response.success){
                get_current_task().current_streamer = $('#streamerSelect').val();
            }
        }
    });
}

// 切换缓存数
function selectCacheCount(){
    if(!$('#countSelect').val()){
        return;
    }

    $.ajax({
        url: '/selectCacheCount',
        type: 'POST',
        data: {
            scene_idx : get_current_task().scene_name,
            cache_count: $('#countSelect').val()
        },
        success: function(response) {
            if(response.success){
                get_current_task().cache_count = $('#countSelect').val();
            }
        }
    });
}


// 切换平台
function selectLivePlatform(){
    if(!$('#livePlatformSelect').val()){
        return alert('请选择一下开播平台');
    }

    $.ajax({
        url: '/selectLivePlatform',
        type: 'POST',
        data: {
            scene_idx : get_current_task().scene_name,
            live_platform: $('#livePlatformSelect').val()
        },
        success: function(response) {
            if(response.success){
                get_current_task().live_platform = $('#livePlatformSelect').val();
            }
        }
    });
}


// 选择场景
function selectScene(scene_idx){
    $.ajax({
        url: '/selectScene',
        type: 'POST',
        data: {
            scene_idx : get_current_task().scene_name
        },
        success: function(response) {
            if(response.success){
                
            }
        }
    });
}

function get_current_task(){
    var current_task = {}

    for(var k in task_config){
        if(task_config[k]['active']){
            current_task = task_config[k];
        }
    }

    return current_task;
}

// 在这里更新各种组件信息
function update_task_config(task_config){
    current_task = get_current_task();

    if(current_task.startGenScript){
        $('#startGenScript').text('正在生成' + current_task.current_product + '的话术...');
    } else {
        $('#startGenScript').text('开始生成话术');
    }
    
    // 更新产品
    $('#productSelect').val(current_task.current_product);

    // 更新音色
    $('#streamerSelect').val(current_task.current_streamer);

    // 更新缓存数
    $('#countSelect').val(current_task.cache_count);

    // 更新开播平台
    $('#livePlatformSelect').val(current_task.live_platform);

    $('#clearScript').text('清空话术');
    
    $('#stopGenScript').text('暂停生成话术');
}

// 查询话术库存
function getScriptCount(){
    $.ajax({
        url: '/getScriptCount',
        type: 'POST',
        data: {
            scene_idx : get_current_task().scene_name
        },
        success: function(response) {
            if(response.count > -1){
                $('#startGenScript').text($('#startGenScript').text().replace(/（\d+）/g,'') + '（' + response.count + '）');
            }
        }
    });
}

$(document).ready(function(){
    $.ajax({
        url: '/update_task_config',
        type: 'POST',
        data: {

        },
        success: function(response) {
            task_config = response;

            update_task_config(task_config);
        }
    });
    
    // 切换tab
    $('#myTab .nav-link').click(function(e) {
        $('#myTab .nav-link').removeClass('active');

        $(this).addClass('active');

        for(var k in task_config){
            task_config[k]['active'] = false;
        }

        current_scene_idx = $(this).attr('aria-controls');

        task_config[current_scene_idx]['active'] = true;

        update_task_config(task_config);
    });

    setInterval(function(){
        getScriptCount();
    },1000);
});

